package nl.hu.modellingengine.ribbon.server;

import java.io.File;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import nl.hu.modellingengine.ribbon.client.RibbonService;
import nl.hu.modellingengine.shared.DiagramType;
import nl.hu.modellingengine.shared.ElementType;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class RibbonServiceImpl extends RemoteServiceServlet implements
		RibbonService {

	private static final long serialVersionUID = 6220846759130607350L;

	@Override
	public ArrayList<ElementType> getElementTypes(String language, String version) {
		ArrayList<ElementType> elementTypes = new ArrayList<ElementType>();
		try{
			File filXmlFile = new File("languages/" + language + "_" + version + "/" + language + ".xml");
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder dbBuilder = dbFactory.newDocumentBuilder();
			Document doc = dbBuilder.parse(filXmlFile);
			
			NodeList nListDiagrams = doc.getElementsByTagName("diagram");
			
			for(int i=0;i< nListDiagrams.getLength();i++){
				Node nNodeDiagram = nListDiagrams.item(i);
				NodeList nListDiagram = nNodeDiagram.getChildNodes();
				
				DiagramType diagramType = new DiagramType();
				
				for(int x=0;x<nListDiagram.getLength();x++){
					Node nNodeTempDiagram = nListDiagram.item(x);
					if(nNodeTempDiagram.getNodeName().equals("name")){
						diagramType.setStrName(nNodeTempDiagram.getTextContent());
					}
					if(nNodeTempDiagram.getNodeName().equals("description")){
						diagramType.setStrDescription(nNodeTempDiagram.getTextContent());
					}
					if(nNodeTempDiagram.getNodeName().equals("elements")){
						Element eElementTemp = (Element) nNodeTempDiagram;
						NodeList nListElements = eElementTemp.getElementsByTagName("element");
						
						for(int y=0;y<nListElements.getLength();y++){
							Node nNodeElementTypes = nListElements.item(y);
							NodeList nListElementTypes = nNodeElementTypes.getChildNodes();
							
							ElementType elementType = new ElementType();
							
							for(int z=0;z<nListElementTypes.getLength();z++){
								Node nNodeTempElement = nListElementTypes.item(z);
								if(nNodeTempElement.getNodeName().equals("name")){
									elementType.setStrName(nNodeTempElement.getTextContent());
								}
								if(nNodeTempElement.getNodeName().equals("description")){
									elementType.setStrDescription(nNodeTempElement.getTextContent());
								}
								if(nNodeTempElement.getNodeName().equals("symbol")){
									elementType.setStrImageSource("languages/" + language + "_" + version + "/images/" + nNodeTempElement.getTextContent());
								}
								elementType.setDiagramType(diagramType);
								
							}
							elementTypes.add(elementType);
						}
					}
				}		
			}	
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		return elementTypes;
	}

	@Override
	public ArrayList<DiagramType> getDiagramTypes(String language, String version) {
		ArrayList<DiagramType> diagramTypes = new ArrayList<DiagramType>();
		try{
			File filXmlFile = new File("languages/" + language + "_" + version + "/" + language + ".xml");
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder dbBuilder = dbFactory.newDocumentBuilder();
			Document doc = dbBuilder.parse(filXmlFile);
			
			NodeList nListDiagrams = doc.getElementsByTagName("diagram");
			
			for(int i=0;i< nListDiagrams.getLength();i++){
				Node nNodeDiagram = nListDiagrams.item(i);
				NodeList nListDiagram = nNodeDiagram.getChildNodes();
				
				DiagramType diagramType = new DiagramType();
				
				for(int x=0;x<nListDiagram.getLength();x++){
					Node nNodeTempDiagram = nListDiagram.item(x);
					if(nNodeTempDiagram.getNodeName().equals("name")){
						diagramType.setStrName(nNodeTempDiagram.getTextContent());
					}
					if(nNodeTempDiagram.getNodeName().equals("description")){
						diagramType.setStrDescription(nNodeTempDiagram.getTextContent());
					}
				}
				diagramTypes.add(diagramType);
			}
		}catch(Exception e){
			System.out.println(e.getMessage());
			
		}			
		return diagramTypes;
	}

	@Override
	public ArrayList<ElementType> getElementsOfDiagramType(String language, String version, DiagramType diagramType) {
		// TODO Auto-generated method stub
		return null;
	}

}